package com.czk.shop.mq;

import com.alibaba.fastjson.JSON;
import com.comon.czk.constant.ShopCode;
import com.comon.czk.entity.MQEntity;
import com.czk.shop.mapper.TradeOrderMapper;
import com.czk.shop.pojo.TradeOrder;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/10/20 21:23
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "${mq.order.topic}", consumerGroup = "${mq.order.consumer.group.name}", messageModel = MessageModel.BROADCASTING)
public class CancelMQListener implements RocketMQListener<MessageExt> {
    @Autowired
    private TradeOrderMapper tradeOrderMapper;

    @Override
    public void onMessage(MessageExt messageExt) {
        // 解析数据
        try {
            String body = new String(messageExt.getBody(), "UTF-8");
            MQEntity mqEntity = JSON.parseObject(body, MQEntity.class);
            // 查询订单，更新订单为取消
            TradeOrder tradeOrder = tradeOrderMapper.selectByPrimaryKey(mqEntity.getOrderId());
            // 更新
            tradeOrder.setOrderStatus(ShopCode.SHOP_ORDER_CANCEL.getCode());
            tradeOrderMapper.updateByPrimaryKey(tradeOrder);
            log.info("订单取消成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("订单取消失败");
        }
    }
}
